<%@page import="java.util.Date"%>
<%@page import="qldc.pm.tiensu.service.PhieuThongTinVeNguoiCoTienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.PhieuThongTinVeNguoiCoTienSu"%>
<%@page import="qldc.pm.tiensu.model.DoiTuongTienSu"%>
<%@page import="qldc.pm.tiensu.service.DoiTuongTienSuLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.liferay.portal.util.PortalUtil"%>
<%@page import="qldc.pm.tiensu.service.LichSuXoaAnTichLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.LichSuXoaAnTich"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="qldc.pm.tiensu.service.DoiTuongTienSuDTOLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.dto.DoiTuongTienSuDTO"%>
<%@page import="qldc.pm.tiensu.service.TienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.TienSu"%>
<%@page import="qldc.pm.qtht.dmdc.service.TonGiaoLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.TonGiao"%>
<%@page import="qldc.pm.qtht.dmdc.model.DanToc"%>
<%@page import="qldc.pm.qtht.dmdc.service.DanTocLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.utils.DiaChiUtils"%>
<%@page import="qldc.pm.qtht.dmdc.enumclass.EnumGioiTinh"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="qldc.pm.csdl.cd90.model.dto.CongDanDTO"%>
<%@page import="qldc.pm.csdl.cd90.service.CongDanDTOLocalServiceUtil"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@include file="../../init.jsp"%>

<%
String redirect = ParamUtil.getString(request, "redirect");

PortletURL portletURL = renderResponse.createRenderURL();
portletURL.setParameter("mvcPath", "/html/view.jsp");
portletURL.setParameter("chonChucNangItem", TienSuConstants.QUAN_LY_YEU_CAU_XOA_AN_TICH);
portletURL.setParameter("tabs1", "danh_sach");
String pathThemeImages = themeDisplay.getPathThemeImages();

long tienSuId = ParamUtil.getLong(request, "tienSuId");

String soDinhDanhCaNhan = ParamUtil.getString(request, "soDinhDanhCaNhan");


String hoVaTen = "";
String gioiTinhKey = "";
String soCMND = "";
String soSoHoKhau = "";
String noiThuongTru = "";
String ngaySinh = "";
String danToc = "";
String tonGiao = "";
String quocTich = "";
String hoVaTenCha = "";
String hoVaTenMe = "";

CongDanDTO congDanDTO = CongDanDTOLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

if(Validator.isNotNull(congDanDTO)) {
	hoVaTen = congDanDTO.getHoVaTen();
	
	gioiTinhKey = EnumGioiTinh.getKey(congDanDTO.getGioiTinh());
	
	soCMND = congDanDTO.getSoCMND();
	soSoHoKhau = congDanDTO.getSoSoHoKhau();
	
	long noiThuongTruQuanHuyenId = congDanDTO.getNoiThuongTruQuanHuyenId();
	long noiThuongTruPhuongXaId = congDanDTO.getNoiThuongTruPhuongXaId();
	String noiThuongTruDiaChi = congDanDTO.getNoiThuongTruDiaChi();
	
	noiThuongTru = DiaChiUtils.createBlockDiaChi(noiThuongTruQuanHuyenId, noiThuongTruPhuongXaId, noiThuongTruDiaChi);
	
	ngaySinh = congDanDTO.getNgaySinh();
	
	long danTocId = congDanDTO.getDanTocId();
	if(danTocId > 0) {
		DanToc dmdcDanToc = DanTocLocalServiceUtil.findById(danTocId);
		
		if(Validator.isNotNull(dmdcDanToc)) {
			danToc = dmdcDanToc.getTenDanToc();
		}
	}
	
	long tonGiaoId = congDanDTO.getTonGiaoId();
	if(tonGiaoId > 0) {
		TonGiao dmdcTonGiao = TonGiaoLocalServiceUtil.findById(tonGiaoId);
		
		if(Validator.isNotNull(dmdcTonGiao)) {
			tonGiao = dmdcTonGiao.getTenTonGiao();
		}
	}
	
	quocTich = congDanDTO.getQuocTich();
	hoVaTenCha = congDanDTO.getHoVaTenCha();
	hoVaTenMe = congDanDTO.getHoVaTenMe();
}
String toaAnXuPhat = "";
String ngayPhamToi = "";
String ngayToaAn = "";
String toiDanh = "";
String noiDungXuPhat = "";
String ngayYeuCauXoaAnTich = "";
String coQuanThiHanhAn = "";
String nguoiYeuCauXoaAnTich = "";
String soQuyetDinhThiHanhAn = "";
String ngayQuyetDinhThiHanhAn = "";
String maHoSoXoaAnTich ="";
String banAnSo = "";
String kinhGui = "";
String mode = ParamUtil.getString(request, "mode", "display");

long lichSuXoaAnTichId = 0;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
boolean duocQuyenKyDuyet = false;
TienSu tienSu = TienSuLocalServiceUtil.findByTienSuId(tienSuId);
if (Validator.isNotNull(tienSu)){
	PhieuThongTinVeNguoiCoTienSu phieu = PhieuThongTinVeNguoiCoTienSuLocalServiceUtil.findByTienSuId(tienSuId);
	 if (Validator.isNotNull(phieu)){
		 hoVaTenCha = phieu.getHoTenCha();
		 hoVaTenMe = phieu.getHoTenMe();
		 
	 }
 toaAnXuPhat = tienSu.getToaAnXuPhat();
 ngayToaAn = Validator.isNotNull(tienSu.getNgayToaAn()) ? sdf.format(tienSu.getNgayToaAn()) : "";
 ngayPhamToi = Validator.isNotNull(tienSu.getNgayPhamToi()) ? sdf.format(tienSu.getNgayPhamToi()) : "";
 toiDanh = tienSu.getToiDanh();
 noiDungXuPhat = tienSu.getHinhPhatChinh();
 banAnSo = tienSu.getBanAnSo();
 LichSuXoaAnTich lichSuXoa = LichSuXoaAnTichLocalServiceUtil.findByTienSuId(tienSuId);
 if (Validator.isNotNull(lichSuXoa)){
	 nguoiYeuCauXoaAnTich = lichSuXoa.getNguoiYeuCauXoaAnTich();
	 lichSuXoaAnTichId = lichSuXoa.getLichSuXoaAnTichId();
	 coQuanThiHanhAn = lichSuXoa.getCoQuanThiHanhAn();
	 ngayYeuCauXoaAnTich = Validator.isNotNull(lichSuXoa.getNgayYeuCauXoaAnTich())? sdf.format(lichSuXoa.getNgayYeuCauXoaAnTich()) :"";
	 soQuyetDinhThiHanhAn = lichSuXoa.getSoQuyetDinhThiHanhAn();
	 ngayQuyetDinhThiHanhAn = Validator.isNotNull(lichSuXoa.getNgayQuyetDinhThiHanhAn())?sdf.format(lichSuXoa.getNgayQuyetDinhThiHanhAn()) :"";
	 maHoSoXoaAnTich = lichSuXoa.getMaHoSoXoaAnTich();
	 kinhGui = lichSuXoa.getKinhGui();
	 //User currentUuser = PortalUtil.getUser(actionRequest);

	 duocQuyenKyDuyet = LichSuXoaAnTichLocalServiceUtil.kiemTraQuyetKyDuyetHoSoXoaAnTich(lichSuXoa, user.getUserId());
 }
}
if (ngayYeuCauXoaAnTich.isEmpty()){
	ngayYeuCauXoaAnTich = sdf.format(new Date());
}
%>


<portlet:resourceURL var="layMaHoSoXoaAnTich" id="layMaHoSoXoaAnTich">
</portlet:resourceURL>

<script type="text/javascript">

function <portlet:namespace/>layMa() {
	jQuery.ajax({
		url : "<%=layMaHoSoXoaAnTich.toString()%>",
		dataType : 'json',
		type : "POST",
		cache : false,
		timeout : 10000,
		async : false,
		success : function(data) {
			$("#<portlet:namespace/>maHoSoXoaAnTich").val(data.maHoSoXoaAnTich);			
		},
		error : function(xhr, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
	
}

function <portlet:namespace/>validationForm(){
	var isOk = true;	
	var maso = $('#<portlet:namespace/>maHoSoXoaAnTich');
	
	if (maso.val() == '') {
		showPopupConfirm('<liferay-ui:message key="xin-vui-long-lay-ma-ho-so-xoa-an-tich" />',['Ok'],function(index) {
			maso.focus();
		});
		
		isOk = false;
	} else {
		var inputList = $(".mandatory");	
		
		if (inputList.length > 0){
			for (var i = 0;i<inputList.length;i++) {
				var firstItem = inputList.eq(i);
				if (firstItem.val() == ''){
					var messageName = firstItem.attr('message').toString();
					
					showPopupConfirm('<liferay-ui:message key="xin-vui-long-nhap-thong-tin" />  : ['+messageName+']',['Ok'],function(index) {
						$(firstItem).focus();
						isOk = false;
						return;
					});
					
					isOk = false;
					break;
				}
			}	
		}
	}
	
	var ngayYeuCauXoaAnTich = $("#<portlet:namespace/>ngayYeuCauXoaAnTich");
	if(isSelectedDateAfterToday(ngayYeuCauXoaAnTich.val())) {
		showPopupConfirm('<liferay-ui:message key="ngay-lap-ho-so-xoa-an-tich-khong-dung-vui-long-chon-ngay-truoc-ngay-hien-tai" />', ['Ok'], function(index) {
			ngayYeuCauXoaAnTich.focus();
		});
		
		isOk = false;
	}
	
	return isOk;
}
function <portlet:namespace/>kyDuyetHoSoXoaAnTich(){
	$('#<portlet:namespace/>fm').removeAttr("target");

	var co = '<liferay-ui:message key="co" />';
 	var khong = '<liferay-ui:message key="khong" />';
 	var mess = '<liferay-ui:message key="ban-co-muon-luu-thong-tin" />';
		showPopupConfirm(mess,[co, khong], function(index) {
		    if (index == 0){
		    	$("#<portlet:namespace/>cmd").val('<%=TienSuConstants.KY_DUYET_HO_SO_XOA%>')
		    	$("#<portlet:namespace/>fm").submit();
			} 
		});
 	
}

function <portlet:namespace/>saveXoaAnTich() {
	$('#<portlet:namespace/>fm').removeAttr("target");
	var co = '<liferay-ui:message key="co" />';
 	var khong = '<liferay-ui:message key="khong" />';
 	var mess = '<liferay-ui:message key="ban-co-muon-luu-thong-tin" />';
 	if(<portlet:namespace/>validationForm()){
		showPopupConfirm(mess,[co, khong], function(index) {
		    if (index == 0){
		    	$("#<portlet:namespace/>cmd").val('<%=TienSuConstants.LUU%>')
		    	$("#<portlet:namespace/>fm").submit();
			} 
		});
 	}
}

function <portlet:namespace/>inHoSo() {	
	    	$("#<portlet:namespace/>cmd").val('<%=TienSuConstants.IN%>')
	    	$("#<portlet:namespace/>fm").submit();
	    	$("#<portlet:namespace/>fm").attr('target','_blank')
		
}
$(function() {	 
	$("#<portlet:namespace/>ngayQuyetDinhThiHanhAn").datepicker({
		showOn: "both",
		buttonImage: "<%=pathThemeImages%>/common/calendar.png",
			buttonImageOnly : true,
			buttonText : "<liferay-ui:message key="ngay-lap-ho-so-xoa-an-tich" />",
			dateFormat : "dd/mm/yy"
			
		});
	$("#<portlet:namespace/>ngayYeuCauXoaAnTich").datepicker({
		showOn: "both",
		buttonImage: "<%=pathThemeImages%>/common/calendar.png",
			buttonImageOnly : true,
			buttonText : "<liferay-ui:message key="ngay-lap-ho-so-xoa-an-tich" />",
			dateFormat : "dd/mm/yy"
			
		});
	
});
</script>
<portlet:actionURL var="xoaAnTichActionRUL">
	<portlet:param name="<%=TienSuConstants.ACTION_HANDLER_PARAM %>" value="<%=TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU %>"/>
	<portlet:param name="<%=Constants.ACTION %>" value="xoaAnTich"/>
	<portlet:param name="tienSuId" value="<%=String.valueOf(tienSuId) %>"/>
	<portlet:param name="lichSuXoaAnTichId" value="<%=String.valueOf(lichSuXoaAnTichId) %>"/>
	<portlet:param name="redirect" value="<%=portletURL.toString() %>"/>
</portlet:actionURL>

<liferay-ui:header title="thong-tin-nguoi-co-tien-su" showBackURL="<%=false %>"></liferay-ui:header>
<table class="gridnormal">
	<tr>
		<td class="txtlabel aui-w20"><liferay-ui:message key="ho-va-ten" /></td>
		<td class="aui-w30"><font style="font-weight: bold;"><%=hoVaTen %></font></td>
		<td class="txtlabel aui-w20"><liferay-ui:message key="gioi-tinh" /></td>
		<td class="aui-w30"><font style="font-weight: bold;"><liferay-ui:message key="<%=gioiTinhKey %>"></liferay-ui:message></font></td>
	</tr>
	
	<tr>
		<td><liferay-ui:message key="so-dinh-danh-ca-nhan" /></td>
		<td><font style="font-weight: bold;"><%=soDinhDanhCaNhan %></font></td>
		<td><liferay-ui:message key="so-cmnd" /></td>
		<td><font style="font-weight: bold;"><%=soCMND %></font></td>
	</tr>

	<tr>
		<td><liferay-ui:message key="so-so-ho-khau" /></td>
		<td><font style="font-weight: bold;"><%=soSoHoKhau %></font></td>
		<td><liferay-ui:message key="ngay-sinh" /></td>
		<td><font style="font-weight: bold;"><%=ngaySinh %></font></td>
	</tr>
	
	<tr>
		<td><liferay-ui:message key="noi-thuong-tru" /></td>
		<td><font style="font-weight: bold;"><%=noiThuongTru %></font></td>
		<td><liferay-ui:message key="dan-toc" /></td>
		<td><font style="font-weight: bold;"><%=danToc %></font></td>
	</tr>
	
	<tr>
		<td><liferay-ui:message key="ton-giao" /></td>
		<td><font style="font-weight: bold;"><%=tonGiao %></font></td>
		<td><liferay-ui:message key="quoc-tich" /></td>
		<td><font style="font-weight: bold;"><%=quocTich %></font></td>
	</tr>
	
	<tr>
		<td><liferay-ui:message key="ho-va-ten-cha" /></td>
		<td><font style="font-weight: bold;"><%=hoVaTenCha %></font></td>
		<td><liferay-ui:message key="ho-va-ten-me" /></td>
		<td><font style="font-weight: bold;"><%=hoVaTenMe %></font></td>
	</tr>
</table>

<liferay-ui:header title="thong-tin-tien-su" showBackURL="<%=false %>"></liferay-ui:header>
<table class="gridnormal">
	<tr>
		<td class="txtlabel aui-w20"><liferay-ui:message key="toa-an-xu-phat" /></td>
		<td class="aui-w30"><font style="font-weight: bold;"><%=toaAnXuPhat %></font></td>
		<td class="txtlabel aui-w20"><liferay-ui:message key="ban-an-so" /></td>
		<td class="aui-w30"><font style="font-weight: bold;"><%=banAnSo %></font></td>
	</tr>
	
	<tr>
		<td><liferay-ui:message key="ngay-toa-an" /></td>
		<td colspan="3"><font style="font-weight: bold;"><%=ngayToaAn %></font></td>
		
	</tr>
	
	<tr>
		<td><liferay-ui:message key="toi-danh" /></td>
		<td colspan="3"><font style="font-weight: bold;"><%=toiDanh %></font></td>
	</tr>
	
	
	<tr>
		<td><liferay-ui:message key="noi-dung-xu-phat" /></td>
		<td colspan="3"><font style="font-weight: bold;"><%=noiDungXuPhat %></font></td>
	</tr>
</table>

<form name="<portlet:namespace />fm" id="<portlet:namespace />fm" action="<%=xoaAnTichActionRUL.toString() %>" method="post" target="_blank" >
	<input name="<portlet:namespace/>hoVaTenDoiTuongTienSu" type="hidden" value="<%=hoVaTen%>">
	<input id="<portlet:namespace/>cmd" name="<portlet:namespace/>cmd" type="hidden">
	
	<table class="gridnormal">
			<tr>
					<td class="aui-w20"><liferay-ui:message key="ma-ho-so-xoa-an-tich" />&nbsp;<font color="red">*</font></td>
					<td class="aui-w30">
						<input type="text" required id="<portlet:namespace/>maHoSoXoaAnTich" name="<portlet:namespace/>maHoSoXoaAnTich" style="width: 50%" readonly="readonly" value="<%=maHoSoXoaAnTich%>"/>
						<c:if test='<%= mode.equalsIgnoreCase("edit") %>'>
						<input type="button" class="btn" id="<portlet:namespace/>layMaHoSoXoaAnTich" value="<liferay-ui:message key="lay-so" />" onclick="<portlet:namespace/>layMa();"/>
						</c:if>
					</td>
					
					
			
		</tr>
		<tr>
			<td class="txtlabel aui-w20"><liferay-ui:message key="co-quan-thi-hanh-an-dan-su" />&nbsp;<font color="red">*</font></td>
			<td colspan="3" class="aui-w30"><input  class="mandatory" message="<liferay-ui:message key='co-quan-thi-hanh-an-dan-su' />"  style="width: 97%" placeholder="<liferay-ui:message key='ghi-ten-co-quan-thi-hanh-an-dan-su' />" <%=mode.equals("display") ? "readonly='readonly'" :"" %>  type="text" name="<portlet:namespace/>coQuanThiHanhAn" value="<%=coQuanThiHanhAn %>" /></td>
			
		</tr>
		<tr>
			<td class="txtlabel aui-w20"><liferay-ui:message key="ngay-lap-ho-so-xoa-an-tich" />&nbsp;<font color="red">*</font></td>
			<td class="aui-w30 ip-datepicker"><input  <%=mode.equals("display") ? "readonly='readonly'" :"" %>  class="mandatory" message="<liferay-ui:message key='ngay-lap-ho-so-xoa-an-tich' />"   style="width:90%" type="text" name="<portlet:namespace/>ngayYeuCauXoaAnTich" id="<portlet:namespace/>ngayYeuCauXoaAnTich"  value="<%=ngayYeuCauXoaAnTich%>" /></td>
			<td><liferay-ui:message key="nguoi-lap" />&nbsp;<font color="red">*</font></td>
			<td><input <%=mode.equals("display") ? "readonly='readonly'" :"" %>  class="mandatory" message="<liferay-ui:message key='nguoi-lap' />"  type="text" style="width:91%" name="<portlet:namespace/>nguoiYeuCauXoaAnTich" id="<portlet:namespace/>nguoiYeuCauXoaAnTich"  value="<%=nguoiYeuCauXoaAnTich%>" /></td>
		</tr>
		<tr>
			<td class="txtlabel aui-w20"><liferay-ui:message key="kinh-gui" />&nbsp;<font color="red">*</font></td>
			<td colspan="3" class="aui-w30"><input  class="mandatory" message="<liferay-ui:message key='kinh-gui' />"  <%=mode.equals("display") ? "readonly='readonly'" :"" %> style="width:97%" type="text" placeholder="<liferay-ui:message key="so-tu-phap-tinh-thanh-pho-noi-thi-hanh-an" />" id="<portlet:namespace/>kinhGui"  name="<portlet:namespace/>kinhGui" value="<%=kinhGui%>" /></td>
			
		</tr>
		<tr>
			<td class="txtlabel aui-w20"><liferay-ui:message key="chap-hanh-xong-hinh-phat" />&nbsp;<font color="red">*</font></td>
			<td class="aui-w30" colspan="3">
				<textarea style="height:100px;width:97%" id="<portlet:namespace />hinhPhat"  class="mandatory" message="<liferay-ui:message key='chap-hanh-xong-hinh-phat' />"   <%=mode.equals("display") ? "readonly='readonly'" :"" %>  name="<portlet:namespace />hinhPhat"><%=noiDungXuPhat %></textarea>
			</td>
			
		</tr>
		<tr>
			<td class="txtlabel aui-w20"><liferay-ui:message key="so-quyet-dinh-thi-hanh-an" />&nbsp;<font color="red">*</font></td>
			<td class="aui-w30"><input type="text"  class="mandatory" message="<liferay-ui:message key='so-quyet-dinh-thi-hanh-an' />"  <%=mode.equals("display") ? "readonly='readonly'" :"" %>  name="<portlet:namespace/>soQuyetDinhThiHanhAn" id="<portlet:namespace/>soQuyetDinhThiHanhAn"  value="<%=soQuyetDinhThiHanhAn%>" /></td>
			<td><liferay-ui:message key="ngay-quyet-dinh-thi-hanh-an" />&nbsp;<font color="red">*</font></td>
			<td class="aui-w30 ip-datepicker"><input  class="mandatory" message="<liferay-ui:message key='ngay-quyet-dinh-thi-hanh-an' />"  <%=mode.equals("display") ? "readonly='readonly'" :"" %>   style="width:91%" type="text" name="<portlet:namespace/>ngayQuyetDinhThiHanhAn" id="<portlet:namespace/>ngayQuyetDinhThiHanhAn"  value="<%=ngayQuyetDinhThiHanhAn%>" /></td>
		</tr>
				
		<tr>
			<td></td>
			<td colspan="3">
				<div align="left">
				<%
				if (mode.equals("display")){%>
						<input type=button class="btn btn-primary" value="<liferay-ui:message key="in" />" onclick="<portlet:namespace/>inHoSo();"/>
				<%
				if (duocQuyenKyDuyet) {
					%>
					<input type=button class="btn btn-primary" value="<liferay-ui:message key="ky-duyet-ho-so" />" onclick="<portlet:namespace/>kyDuyetHoSoXoaAnTich();"/>
					
				<%}
				}
				
				else {
					%>
				
					<input type=button class="btn btn-primary" value="<liferay-ui:message key="lap-ho-so-xoa-an-tich" />" onclick="<portlet:namespace/>saveXoaAnTich();"/>
					<%}
				%>
					<input type="button" class="btn" onclick="javascript:submitForm(document.hrefFm,'<%=redirect %>');" value="<liferay-ui:message key="tro-ve" />" />
				</div>
			</td>
		</tr>
	</table>
</form>

